<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="author" content="郝珍华">
    <title></title>
    <style>
        body {
            margin: 0;
        }

        ul {
            list-style: none;
            margin: 0;
            padding: 0;
        }

        .container {
            width: 200px;
            margin: 200px auto;
        }

        ul li {
            margin: 10px 0;
            font-size: 25px;
        }

        ul li input {
            width: 20px;
            height: 20px;
        }

        p {
            display: inline-block;
            margin-left: 10px;
            color: blue;
            cursor: pointer;
        }
    </style>
</head>

<body>
    <div class="container">
        <ul>
            <li><input type="checkbox" id="one"><span>全选</span></input>
                <p>反选</p>
            </li>
            <li><input type="checkbox" class="li">选择01</input>
            </li>
            <li><input type="checkbox" class="li">选择02</input>
            </li>
            <li><input type="checkbox" class="li">选择03</input>
            </li>
            <li><input type="checkbox" class="li">选择04</input>
            </li>
            <li><input type="checkbox" class="li">选择05</input>
            </li>
            <li><input type="checkbox" class="li">选择06</input>
            </li>
            <li><input type="checkbox" class="li">选择07</input>
            </li>
            <li><input type="checkbox" class="li">选择08</input>
            </li>
            <li><input type="checkbox" class="li">选择09</input>
            </li>
            <li><input type="checkbox" class="li">选择10</input>
            </li>
        </ul>
    </div>



</body>

<script>
    var one = document.getElementById('one');
    var span = document.querySelector('span');
    var lis = document.querySelectorAll('.li');
    var p = document.querySelector('p');

    one.onclick = function () {
        if (span.innerText == '全不选') {
            for (var i = 0; i < lis.length; i++) {
                lis[i].checked = false;
                span.innerText = '全选'
            }
        } else {
            for (var i = 0; i < lis.length; i++) {
                lis[i].checked = true;
                span.innerText = '全不选'
            }
        }
    }
    var index;
    p.onclick = function () {
        for (var i = 0; i < lis.length; i++) {
            if (lis[i].checked == true) {
                lis[i].checked = false;
            } else {
                lis[i].checked = true;
                one.checked = true;
            }
        }
        if (span.innerText == '全不选') {
            span.innerText = '全选'
            one.checked = false;
        }
        for (var i = 0; i < lis.length; i++) {
            if (lis[i].checked == false) {
                console.log('123');
                one.checked = false;
                span.innerText = '全选';
                return;
            } else {
                console.log('123456');
                span.innerText = '全不选';
                one.checked = true;
            }
        }

    }
    for (var i = 0; i < lis.length; i++) {
        lis[i].onclick = function () {
            for (var i = 0; i < lis.length; i++) {
                if (lis[i].checked == false) {
                    return;
                }
            }
            span.innerText = '全不选';
            one.checked = true;
        }
    }

</script>

</html>